<?php
$check = true;
$errors = array();

require_once 'library/config.php';

function __autoload($class_name) {
    include_once 'library/' . strtolower($class_name) . '.php';
}

if (!$_POST['username'] || !$_POST['password']) {
    $check = false;

    if (empty($_POST['username']))
        array_push($errors, 'Username is required!');

    if (empty($_POST['username']))
        array_push($errors, 'Password is required!');
}
else {
    $user = new User();
    if ($user->login($_POST['username'], $_POST['password'])) {
        header('Location: index.php');
    } else {
        $check = false;
        array_push($errors, 'Invalid username or password');
    }
}
?>

<?php if (false === $check): ?>
    <!DOCTYPE html>

    <html lang="en-US">
        <head>
            <title>Simple Registration System</title>
        </head>
        <style>
            body { font: normal 14px Verdana; }
            h1 { font-size: 24px; }
            h2 { font-size: 18px; }
        </style>
        <body>

            <div id="wrap">

                <section id="main">

                    <h1>Unable to login</h1>
                    <p>Please try again.</p>

                    <?php
                    if (!empty($errors)) {
                        echo '<ul>';

                        foreach ($errors as $e) {
                            echo '<li>' . $e . '</li>';
                        }

                        echo '</ul>';
                    }
                    ?>

                </section>

            </div>

        </body>
    </html>
<?php endif; ?>